package issr.boundary.vaadin;

import java.util.HashMap;

import com.vaadin.Application.UserChangeEvent;
import com.vaadin.Application.UserChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import issr.boundary.PannelloHome;
import issr.boundary.PannelloLogin;
import issr.boundary.PannelloLogout;
import issr.entity.Utente;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Handler extends VerticalLayout implements UserChangeListener {

    // Portions of the application that exist at all times.
    private Header header;
    // Components shown to logged users.
    private Menu menu;
    private HorizontalSplitPanel menusplit;
    // Used to keep track of the current main view.
    private HashMap<String, AbstractView> viewList = new HashMap<String, AbstractView>();

    /*
     * Constructor of the handler. Creates and styles all default components.
     * 
     * @param window main window of the application
     */
    public Handler(Window window) {
        header = new Header();
        menusplit = new HorizontalSplitPanel();

        setSizeFull();
        setMargin(true);

        // Add the main components for the UI.
        addComponent(header);
        addComponent(menusplit, 1);
        menusplit.setSplitPosition(300, HorizontalSplitPanel.UNITS_PIXELS);
        menusplit.setSizeFull();

        // Visual modifiers of the splitpanel.
        setExpandRatio(menusplit, 1);
        setComponentAlignment(header, Alignment.TOP_CENTER);

        menu = new Menu();
        menusplit.setFirstComponent(menu);
        menusplit.setSecondComponent(new PannelloLogin());

//        setMainView(pannelloLogin);
        this.setSpacing(true);
        window.setLayout(this);
    }

    /*
     * All the UI logic in the event that a user logs in.
     */
    public void userLoggedIn() {
        removeComponent(menusplit.getSecondComponent());
        header.addLogoutComponent(new PannelloLogout((Utente) MainApplication.getProject().getUser()));
        setMainView(new PannelloHome());
    }

    /*
     * Reverts back to the default view and removes all the user specific
     * content.
     */
    public void userLoggedOut() {
        // Tell the other UI components that the user logged off.
        removeComponent(menusplit.getSecondComponent());
        menu.depopulate();
        header.removeLogoutComponent();
        viewList.clear();
        menusplit.setSecondComponent(new PannelloLogin());
    }

    /*
     * Updates the main view when the user clicks an item in the menu.
     * 
     * @param event Event
     */
    public void switchView(String viewName) {
        setMainView(viewList.get(viewName));
    }

    /*
     * Helper method for setting the main view.
     * 
     * @param c View that we want to display
     */
    public void setMainView(AbstractView c) {
        menusplit.setSecondComponent(c);
        try {
            c.open();
        } catch (Exception ex) {
            Logger.getLogger(Handler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*
     * Closes the view and removes it from the menu.
     */
    public void closeCurrentView() throws Exception {
        // Remove the view from the view list.
//        viewList.remove(menu.getTree().getValue().toString());
//        menu.removeCurrentTreeSelection();
    }

    public void applicationUserChanged(UserChangeEvent event) {
        if (event.getNewUser() == null) {
            userLoggedOut();
        } else {
            userLoggedIn();
        }
    }

    public Header getHeader() {
        return header;
    }

    public Menu getMenu() {
        return menu;
    }

    public HashMap<String, AbstractView> getViewList() {
        return viewList;
    }
}
